/**
 * 
 * 深圳市店联电子商务
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.systemView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TGift;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.RPJButtonRoundGray;
import com.ruipengkj.component.RPJButtonRoundOrager;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.GiftModel;
import com.ruipengkj.service.TGiftService;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.baseDataView.GiftAddDialog;
import com.ruipengkj.view.baseDataView.GiftUpdateDialog;
/**
 * 说明：
 * 满就送UI
 * @Author：朱攀
 * @Date：2012-07-12
 */
public class SelectGiftPanel extends BasePagePanel<TGift> implements Runnable{

	private static final long serialVersionUID = 1L;
	//--------------------定义弹出框-----------------------------------
	private GiftAddDialog giftAddDialog;// 增加满就送面板
	private GiftUpdateDialog giftUpdateDialog;// 增加满就送面板
	
	
	//private JLabelJTextFieldComponent cardNumTF = null; //卡号
	//private JLabelJTextFieldComponent nameTF = null;//姓名
	//private JLabelJTextFieldComponent tel = null;//电话
	//private DataPackageComponent startTimeTF = null;//发卡时间
	//private DataPackageComponent timeTF = null;//时间
	
	
	private JButton addButton = null;//增加按钮
	private JButton updateButton =null;   // 修改
	private JButton delButton =null;   // 删除按钮

	private JTable table;
	private GiftModel giftModel;
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TGift> pageView;//翻页主键
	private JScrollPane tableJsp;
	private TGiftService  tGiftService;
	
	//private MenberDetailDialog menberDetailDialog;//会员详细窗体
	
	public SelectGiftPanel() {
		tGiftService=BuildSpringApplication.getInstance().getTGiftServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
//		ApplicationContext.CardUtilThread.setFlag(false);//停止之前线程
//		ApplicationContext.CardUtilThread=new CardUtil(){//重新定义线程
//			protected void send(String drive){
//				//cardNumTF.getText().setText(drive);
//				//cardNumTF.requestFocus();
//			}
//		};
//		ApplicationContext.CardUtilThread.startOn(cardNumTF.getText());//启动线程
//		//ApplicationContext.CardUtilThread.run();//启动线程
	}
	
	private void init() {
		pageView=new PageView<TGift>();
		giftModel=new GiftModel(); 
		table=uiFactory.createRPJTable(giftModel, null, this, 30, JLabel.CENTER);
		//table设置列宽度 AUTO_RESIZE_SUBSEQUENT_COLUMNS 填满父容器  JTable.AUTO_RESIZE_OFF 自适应
		table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		//this.add(this.topPanel(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		//this.add(pagingComponent, BorderLayout.SOUTH);
		
		JPanel gardPanel=uiFactory.createGridJPanel(1, 2);
		JPanel jp1=uiFactory.createFlowJPanel(FlowLayout.LEFT);
		addButton = new RPJButtonRoundBlue("添加(Ctrl+A)");
		updateButton = new RPJButtonRoundGray("修改(Ctrl+M)");
		delButton = new RPJButtonRoundOrager("删除(Ctrl+D)");
		jp1.add(new JLabel("   "));
		jp1.add(addButton);
		jp1.add(updateButton);
		jp1.add(delButton);
		gardPanel.add(jp1);
		gardPanel.add(pagingComponent);
		this.add(gardPanel,BorderLayout.SOUTH);
		
		
		//排序
		TableRowSorter<GiftModel> tableRowSorter = new TableRowSorter<GiftModel>(giftModel);  
		table.setRowSorter(tableRowSorter);
		termQuery(1) ;
	}

//	private JPanel topPanel(){
//		//JPanel jp=new JPanel(new FlowLayout(FlowLayout.LEFT));
//		JPanel jp=uiFactory.createFlowJPanel(FlowLayout.LEFT);
////		tel = new JLabelJTextFieldComponent("手机：", 15);
////		cardNumTF = new JLabelJTextFieldComponent("卡号：", 15);
////		nameTF = new JLabelJTextFieldComponent("姓名：", 15);
////		startTimeTF = new DataPackageComponent("发卡时间：", 15);
////		timeTF = new DataPackageComponent("时间：", 15);
//		
//		addButton = new RPJButtonBlue("增加");
//		updateButton =new RPJButtonBlue("修改");
//		delButton =new RPJButtonBlue("删除");
//		
//		jp.add(addButton);
//		jp.add(updateButton);
//		jp.add(delButton);
//		
//	
//		return jp;
//	}

	private void initListener(){
		
		//-------------------------------------------
		
		addButton.addActionListener(this);
		addButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		delButton.addActionListener(this);
		delButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		updateButton.addActionListener(this);
		updateButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	//条件查询
	protected void termQuery(final int currentPage) {
		final TGift tgift=new TGift();
	
//		tuser.setCardNum(cardNumTF.getText().getText().trim());
//		tuser.setRealName(nameTF.getText().getText().trim());
//		tuser.setSendCardTime(startTimeTF.getDate());
//		tuser.setMobile(tel.getText().getText());
//		tuser.setEndTime(timeTF.getDate());
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TGift> pageModel=tGiftService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tgift);
					
					
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						Util.clearTableRowSorter(table);
						giftModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								Util.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TGift> getPageView() {
		return pageView;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==addButton){
			//增加优惠卷
			addGift();
		}
		
	else if(e.getSource()==delButton){ //---------------------------------------------------
			// 删除优惠卷
			this.deleteGift();
			
		}
		else if (e.getSource() == updateButton) {
			// 修改按钮
			modifyGift();
		} 
	}
	
	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_ENTER){
			//if(e.getSource()==nameTF.getText()||e.getSource() == tel.getText()){
			if(e.getSource() == addButton){
				//增加优惠卷
				addGift();
			} else if (e.getSource() == table) { //--------------------------------------------------
				if (e.getKeyCode() == KeyEvent.VK_DELETE) {
					// 删除商品
					//this.deleteUser();
				}
			}
			else if (e.getSource() == updateButton) {
				// 修改按钮
				//this.modifyUser();
				modifyGift();
			} 
		}
		
	}
	@Override
	public void mouseClicked(MouseEvent e) {
    	if(e.getSource()==table&&e.getClickCount() == 2){
    		//表格
    		String id=table.getValueAt(table.getSelectedRow(), 0).toString();
    		try {
				TGift gift=tGiftService.getByIdService(id);
				if(gift==null){
					JOptionPane.showMessageDialog(null, "当前信息不存在");
					return;
				}
				
				//menberDetailDialog=new MenberDetailDialog(null, "会员详细", true);
	    		//menberDetailDialog.showDialog(user);
				
//				giftUpdateDialog = new GiftUpdateDialog(null, "修改慢就送", true);
//				giftUpdateDialog.showDialog(gift);
				giftAddDialog = new GiftAddDialog(null, "满就送", true);
				giftAddDialog.showDialog(gift);
				if(giftAddDialog.isSuccess==true){
					this.termQuery(1);
				}
				
			} catch (Exception e1) {
				e1.printStackTrace();
			}
    	}
    }
	//------------------------------------------------------------------------------------
	
	
	//增加优惠卷
	public void  addGift(){
		try {
			giftAddDialog = new GiftAddDialog(null, "满就送", true);
			giftAddDialog.showDialog();
			if(giftAddDialog.isSuccess==true){
				this.termQuery(1);
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	
	// 删除选中的优惠卷
	private void deleteGift() {
		if (table.getRowCount() != 0) {
			int row = table.getSelectedRow();
			if (row != -1) {
				if (Util.showConfirmDialog("是否要删除选择记录") == 0) {
					String giftID = (String) table.getValueAt(row, 0);
					try {
						tGiftService.deleteService(giftID);
						JOptionPane.showMessageDialog(null, "删除成功");
						this.termQuery(1);
					} catch (Exception e1) {
						JOptionPane.showMessageDialog(null, "删除失败");
						e1.printStackTrace();
					}
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择行在进行删除");
				return;
			}
		}
	}
//----------------------------修改满就送---------------------------
	

	private void modifyGift() {
		if (table.getRowCount() != 0) {
			int row = table.getSelectedRow();
			if (row != -1) {
				String giftid = (String) table.getValueAt(row, 0);
				try {
					TGift gift = tGiftService.getByIdService(giftid);
//					giftUpdateDialog = new GiftUpdateDialog(null, "修改慢就送", true);
//					giftUpdateDialog.showDialog(gift);
					giftAddDialog = new GiftAddDialog(null, "满就送", true);
					giftAddDialog.showDialog(gift);
					
					
					if(giftUpdateDialog.isSuccess==true){
						this.termQuery(1);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择行在进行修改");
				return;
			}
		}
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	
	
}
